package com.se.factory;

import org.springframework.ai.model.SimpleApiKey;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;

public class OpenAiApiFactory {

    public static OpenAiApi getApi(String url, String key){
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        RestClient.Builder restClientBuilder = RestClient.builder();
        WebClient.Builder webClientBuilder = WebClient.builder();
        ResponseErrorHandler handler = new DefaultResponseErrorHandler();

        return new OpenAiApi(
                url,
                new SimpleApiKey(key),
                map,
                "/v1/chat/completions",
                "/v1/embeddings",
                restClientBuilder,
                webClientBuilder,
                handler
        );
    }
}
